// pages/shoping/shoping.js
import { feach } from "../../http/http";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 2,
    checked: false,
    arr: [],
    cartList: [],
    price: "0.00"
  },
  catchCartList() {
    feach("/cart/list").then(res => {
      // console.log(res);
      let arr3 = res.data.data;
      arr3.forEach(v => {
        v.checked = false;
      });
      this.setData({
        cartList: arr3
      });
      // console.log(this.data.cartList);
    });
  },
  clickbox(e) {
    console.log("点击了商品的选择框", e);
    const id = e.detail.item.id;
    const checked = e.detail.item.checked;
    let quanxuanchecke = true;
    this.data.cartList.forEach(v => {
      if (v.id == id) {
        v.checked = checked;
      }
      if (!checked || !v.checked) {
        quanxuanchecke = false;
      }
    });
    const app = getApp();
    const num = app.getPrices({ cartList: this.data.cartList });
    this.setData({
      cartList: this.data.cartList,
      checked: quanxuanchecke,
      price: num
    });
  },
  chekege() {
    console.log("全选按钮", this.data.checked);
    this.data.checked = !this.data.checked;
    let arr = this.data.cartList;
    arr.forEach(v => {
      v.checked = this.data.checked;
    });
    // 获取所有商品数据
    console.log(this.data.cartList);
    const app = getApp();
    const num = app.getPrices({ cartList: this.data.cartList });
    this.setData({
      cartList: arr,
      price: num,
      checked: this.data.checked
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.checked = getApp().getAllChecked({ cartList: this.data.cartList });
    this.catchCartList();
    // 判断用户是否登录
    if (!getApp().token) {
      wx.redirectTo({
        url: "/pages/login/login"
      });
    }
  },
  tijiaotd() {
    console.log("点击了提交按钮");
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
